<?php
declare (strict_types=1);

namespace app\common\model\payment\admin;

use app\common\model\BaseModel;

/**
 * 订单-交易记录模型
 */
class PaymentTransaction extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'payment_transaction';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    protected $append = ['payment_type_text'];

    /**
     * 支付时间
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/7
     *
     * @param $value
     * @return false|string
     */
    public function getPaymentTimeAttr($value)
    {
        return $value ? date($this->dateFormat, $value) : '';
    }

    /**
     * 支付方式
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-04
     *
     * @param    [type]                         $value [description]
     * @param    [type]                         $data [description]
     * @return   [type]                                [description]
     */
    public function getPaymentTypeTextAttr($value, $data)
    {

        $text = $this->getPaymentTypeInfo();

        return $text[$data['payment_type']] ?? '未知';
    }

    protected function getPaymentTypeInfo()
    {
        return [
            'wxpay'  => '微信',
            'alipay' => '支付宝',
            'lcnpay' => '余额',
        ];
    }

    /**
     * 获取支付方式选项
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-04
     *
     * @return   [type]                         [description]
     */
    public function getPaymentTypeOptions()
    {
        $options = [];

        $paymentType = $this->getPaymentTypeInfo();
        foreach ($paymentType as $key => $value) {
            $options[] = ['title' => $value, 'value' => $key];
        }

        return $options;
    }

}
